<template>
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>
    <!-- 渲染角色列表 -->
    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="setUserRoleFn">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getRoleApi } from '@/api/setting'
import { getUserInfo } from '@/api/user'
import { setUserRoleApi } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    roleid: {
      type: [Number, String],
      required: true
    }
  },
  data() {
    return {
      roleList: [],
      roleIds: []
    }
  },
  created() {
    this.getRole()
    this.getuserInfo()
  },
  methods: {
    // 获取所有的角色
    async getRole() {
      const res = await getRoleApi()
      this.roleList = res.data.rows
    },
    // 获取当前员工的已有角色
    async getuserInfo() {
      const res = await getUserInfo(this.roleid)
      this.roleIds = res.data.roleIds
    },
    // 点击给员工分配角色
    async setUserRoleFn() {
      const res = await setUserRoleApi({ id: this.roleid, roleIds: this.roleIds })
      console.log(res)
      this.$message.success(res.message)
      this.$emit('close')
    },
    cancleDialog() {}
  }
}
</script>

<style scoped lang='scss'>
.footer {
  margin-top: 10px;
}
</style>
